/*
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: AccountPresenter.java
 *   @Date: 18-11-29 下午4:05
 *   @Author: ppzhao
 *   @Description:
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 */

package io.bhex.app.account.presenter;

import java.util.List;

import io.bhex.app.R;
import io.bhex.app.base.AppUI;
import io.bhex.app.utils.NetWorkStatus;
import io.bhex.app.utils.NumberUtils;
import io.bhex.baselib.mvp.BaseCoreActivity;
import io.bhex.baselib.mvp.BasePresenter;
import io.bhex.baselib.network.Utils.CodeUtils;
import io.bhex.baselib.network.listener.SimpleResponseListener;
import io.bhex.baselib.utils.ToastUtils;
import io.bhex.sdk.account.AccountInfoApi;
import io.bhex.sdk.account.LoginApi;
import io.bhex.sdk.account.UserInfo;
import io.bhex.sdk.account.bean.UserInfoBean;
import io.bhex.sdk.account.bean.UserLevelInfoResponse;
import io.bhex.sdk.account.bean.enums.ERROR_CODE;
import io.bhex.sdk.config.ConfigApi;
import io.bhex.sdk.config.bean.ConfigBean;
import io.bhex.sdk.point.PointCardApi;
import io.bhex.sdk.point.bean.PointCardBalanceListResponse;
import io.bhex.sdk.trade.TradeApi;
import io.bhex.sdk.trade.bean.OpenOrderResponse;
import io.bhex.sdk.trade.bean.OrderBean;

public class AccountPresenter extends BasePresenter<AccountPresenter.AccountUI> {
    public interface AccountUI extends AppUI {

        void showOrderNum(String num);

        void resetUnloginStatus();

        void showPointView(boolean isVisible);

        void showUserInfo(UserInfoBean data);

        void updateUserLevelVisible(boolean userLevel);

        void updateUserLevelInfo(UserLevelInfoResponse data);
    }

    @Override
    public void onUIReady(BaseCoreActivity activity, AccountUI ui) {
        super.onUIReady(activity, ui);

    }

    @Override
    public void onResume() {
        super.onResume();
        getConfig();
        if (UserInfo.isLogin()) {
            getUserInfo();
//                getCurrentOrdinaryEntrustOrders();
            getPointBalanceList();
            getUserLevelInfo();
        }
    }

        private void getConfig() {
        ConfigApi.getConfig(new SimpleResponseListener<ConfigBean>(){
            @Override
            public void onBefore() {
                super.onBefore();
                getUI().showProgressDialog("","");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                getUI().dismissProgressDialog();
            }

            @Override
            public void onSuccess(ConfigBean response) {
                super.onSuccess(response);
                if (CodeUtils.isSuccess(response)) {
                    if (response.getFunctions()!=null) {
                        getUI().updateUserLevelVisible(response.getFunctions().isUserLevel());
                    } else {
                        getUI().updateUserLevelVisible(false);
                    }
                }
            }

            @Override
            public void onError(Throwable error) {
                super.onError(error);

            }
        });
    }

    /**
     * 获取用户信息
     */
    public void getUserInfo() {
        LoginApi.GetUserInfo(new SimpleResponseListener<UserInfoBean>() {
            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            public void onSuccess(UserInfoBean data) {
                super.onSuccess(data);
                if (CodeUtils.isSuccess(data, true)) {
                    //保存用户数据
                    UserInfo.saveUserInfo(data);
                    getUI().showUserInfo(data);
                } else {
                    if (data.getCode().equals(ERROR_CODE.NO_LOGIN.getCode())) {
                        getUI().resetUnloginStatus();
                    }
                }
            }

            @Override
            public void onError(Throwable error) {
                super.onError(error);
            }
        });
    }

    public void getUserLevelInfo () {

        AccountInfoApi.GetUserLevelInfo(new SimpleResponseListener<UserLevelInfoResponse>(){
            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            public void onSuccess(UserLevelInfoResponse data) {
                super.onSuccess(data);
                if (CodeUtils.isSuccess(data, true)) {
                    getUI().updateUserLevelInfo(data);
                }
            }

            @Override
            public void onError(Throwable error) {
                super.onError(error);
            }
        });
    }

    /**
     * 获取当前委托
     */
    public void getCurrentEntrustOrders() {
        //第一页或者刷新
        TradeApi.RequestOpenOrder(new SimpleResponseListener<OpenOrderResponse>() {
            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            public void onSuccess(OpenOrderResponse response) {
                super.onSuccess(response);
                if (CodeUtils.isSuccess(response, true)) {
                    List<OrderBean> data = response.getArray();
                    int num = 0;
                    if (data != null) {
                        num = data.size();
                    }
                    getUI().showOrderNum(num + "");

                } else {
                    if (response.getCode().equals(ERROR_CODE.NO_LOGIN.getCode())) {
                        getUI().resetUnloginStatus();
                    }
                }
            }

            @Override
            public void onError(Throwable error) {
                super.onError(error);
            }
        });
    }


    /**
     * 请求点卡余额列表
     */
    public void getPointBalanceList() {
        if (!UserInfo.isLogin()) {
            return;
        }
        if (!NetWorkStatus.isConnected(getActivity())) {
            ToastUtils.showShort(getActivity(), getResources().getString(R.string.hint_network_not_connect));
            return;
        }
        PointCardApi.requestPointCardBalanceList(new SimpleResponseListener<PointCardBalanceListResponse>() {
            @Override
            public void onBefore() {
                super.onBefore();
                getUI().showProgressDialog("", "");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                getUI().dismissProgressDialog();
            }

            @Override
            public void onSuccess(PointCardBalanceListResponse response) {
                super.onSuccess(response);
                if (CodeUtils.isSuccess(response, true)) {
                    List<PointCardBalanceListResponse.BalanceBean> balanceList = response.getBalanceList();
                    if (balanceList != null) {
                        for (PointCardBalanceListResponse.BalanceBean balanceBean : balanceList) {
                            String total = balanceBean.getTotal();
                            if (NumberUtils.sub(total, "0") > 0) {
                                getUI().showPointView(true);
                                return;
                            }

                        }

                    }
                }
            }

            @Override
            public void onError(Throwable error) {
                super.onError(error);
            }
        });
    }
}
